package org.hgvc.webapp.ets.jsf.beans.agent;

import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.Membership;
import org.hgvc.webapp.ets.spring.service.MembershipService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.FacesUtils;


public class AgentSelectMembershipBean extends AbstractBean{

	public String BEAN_NAME="agentSelectMembershipBean";
	private MembershipService service;
	private List<Membership>membershipList;
	private String NAV_SELECT_ACTION="agentProspectSelectMembership";
	private String action;
	private List<SelectItem> membershipSelectList;
	private Membership membership;

	public void setService(MembershipService service) {
		this.service = service;
	}
	/*-----------------------------------------------------*/

	/*public List<SelectItem> getMembershipSelectList() {
		try{
			membershipList = service.getMembershipList();
			membershipSelectList = new ArrayList<SelectItem>();
			for(Membership membership:membershipList){
				SelectItem item = new SelectItem(membership.getMembershipId(),membership.getName());
				membershipSelectList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return membershipSelectList;
	}*/


	public String navIndSelectAction(){
		action = AgentCreateIndProspectBean.NAV_CREATION;
		return NAV_SELECT_ACTION;
	}

	public String navCompSelectAction(){
		action = AgentCreateCompProspectBean.NAV_CREATE;
		return NAV_SELECT_ACTION;
	}

	public void selectActionEvent(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("membershipId");
		String id = (String)param.getValue();
		for(Membership membership:membershipList){
			if(membership.getMembershipId().equalsIgnoreCase(id)){
				this.membership = membership;
				break;
			}
		}
	}

	public String navProspectCreation(){
		try{
			if(action.equalsIgnoreCase(AgentCreateIndProspectBean.NAV_CREATION)){
				AgentCreateIndProspectBean bean = (AgentCreateIndProspectBean)FacesUtils.getManagedBean(AgentCreateIndProspectBean.BEAN_NAME);
				bean.setMembership(membership);
				return bean.navCreateAction();
			}else{
				AgentCreateCompProspectBean bean = (AgentCreateCompProspectBean)FacesUtils.getManagedBean(AgentCreateCompProspectBean.BEAN_NAME);
				bean.setMembership(membership);
				return bean.navCreateAction();
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	public List<Membership> getMembershipList() {
		try{
			membershipList = service.getMembershipList();
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}

		return membershipList;
	}

}
